package acs;

import io.netty.util.CharsetUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NioClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 5000));
        while (true) {
            int select = selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                if (selectionKey.isConnectable()) {
                	System.out.println("connectthreae:"+Thread.currentThread().getName());
                    SocketChannel client = (SocketChannel) selectionKey.channel();
                    client.configureBlocking(false);
                    if (client.isConnectionPending()) {
                        client.finishConnect();
                        ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
                        writeBuffer.put((LocalDateTime.now() + "连接成功").getBytes());
                        writeBuffer.flip();
                        client.write(writeBuffer);
                        ExecutorService executorService = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
                        executorService.submit(() -> {
                            while (true) {
                                writeBuffer.clear();
                                InputStreamReader input = new InputStreamReader(System.in);
                                BufferedReader br = new BufferedReader(input);
                                String message = br.readLine();
                                writeBuffer.put(message.getBytes());
                                writeBuffer.flip();
                                client.write(writeBuffer);
                                client.register(selector, SelectionKey.OP_READ);
                            }
                        });
                    }
                } else if (selectionKey.isReadable()) {
                	System.out.println("isReadablethread:"+Thread.currentThread().getName());
                    SocketChannel client = (SocketChannel) selectionKey.channel();
                    client.configureBlocking(false);
                    ByteBuffer byteBuffer = ByteBuffer.allocate(10);
                    StringBuilder sb=new StringBuilder(150);
                    while (true) {
                        byteBuffer.clear();
                        int read = client.read(byteBuffer);
                        if (read <= 0) {
                            System.out.println(client + "=" + sb);
                            break;
                        }
                        byteBuffer.flip();
                        String message = new String(byteBuffer.array(), CharsetUtil.UTF_8);
                        sb.append(message);
                    }

                }
                iterator.remove();
            }


        }
    }
}
